package dk.au.cs.bdsi.sms.domain;

import java.util.LinkedList;
import java.util.List;

/**
 * Encapsulates the details needed to send a <em>message</em> over the Short Message Service (SMS).
 */
public class Sms {
	private String from;
	private List<PhoneNumber> to;
	private String message;
	private MessageEncoding encoding;
	private SmsStatus status;
	
	/**
	 * Constructs a new {@link Sms} using the given mandatory fields
	 * @param from Name or number of sender
	 * @param to A list of phone numbers to send to
	 * @param message Message
	 * @param encoding Character encoding to convert message to
	 */
	public Sms(String from, List<PhoneNumber> to, String message,
			MessageEncoding encoding, SmsStatus status) {
		this.from = from;
		setTo(to);
		this.message = message;
		this.encoding = encoding;
		this.status = status;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * @param from the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @return the to
	 */
	public List<PhoneNumber> getTo() {
		return to;
	}

	/**
	 * @param to the to to set
	 */
	public void setTo(List<PhoneNumber> to) {
		this.to = new LinkedList<PhoneNumber>(to);
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the encoding
	 */
	public MessageEncoding getEncoding() {
		return encoding;
	}

	/**
	 * @return the status
	 */
	public SmsStatus getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(SmsStatus status) {
		this.status = status;
	}

	/**
	 * @param encoding the encoding to set
	 */
	public void setEncoding(MessageEncoding encoding) {
		this.encoding = encoding;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((encoding == null) ? 0 : encoding.hashCode());
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((message == null) ? 0 : message.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Sms))
			return false;
		Sms other = (Sms) obj;
		if (encoding != other.encoding)
			return false;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (message == null) {
			if (other.message != null)
				return false;
		} else if (!message.equals(other.message))
			return false;
		if (status != other.status)
			return false;
		if (to == null) {
			if (other.to != null)
				return false;
		} else if (!to.equals(other.to))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Sms [from=");
		builder.append(from);
		builder.append(", to=");
		builder.append(to);
		builder.append(", message=");
		builder.append(message);
		builder.append(", encoding=");
		builder.append(encoding);
		builder.append(", status=");
		builder.append(status);
		builder.append("]");
		return builder.toString();
	}
}